function results = SVMDos(cfg,data,timecoursevariant,SVMpass,results)

crashed = 1;
while crashed~=0
try 
    %random heeft niet veel zin als je 'm net met een constante geseed hebt
    %in SVMrun.m
    randomname   = num2str(int32(rand(1)*10000));
    datafilename = [cfg.subject data.ROI data.id randomname];
    traindatafile=[datafilename 'traindata.dat'];
    testdatafile =[datafilename 'testdata.dat'];

    dimensions=size(data.trainmatrix);
    headerlines={'@kernel',['type ' 'polynomial'],'degree 1','@parameters','pattern',...
    'C 1.000000','L+ 0.010000','L- 0.010000','epsilon+ 0.010000','epsilon- 0.010000',...
    'verbosity 0','@examples',['dim ' num2str(dimensions(1))],'format xy'};

    fidtrain=fopen(traindatafile,'w');
    fidtest=fopen(testdatafile,'w');

    for n=1:length(headerlines)
        fprintf(fidtrain,'%s\n',char(headerlines{n}));
        fprintf(fidtest,'%s\n',char(headerlines{n}));
    end
    dlmwrite(traindatafile,[data.trainmatrix' data.trainevents],'-append','delimiter',' ');
    dlmwrite(testdatafile,[data.testmatrix' data.testevents],'-append','delimiter',' ');
    fclose(fidtrain); fclose(fidtest);
    
        dos(['SVMLearn.exe ' traindatafile]);
        dos(['SVMPredict.exe ' traindatafile '.svm ' testdatafile]);
    copyfile('wtest.dat',[datafilename 'wtest.dat'])
    copyfile('accuracy.dat',[datafilename 'accuracy.dat']) 

    %% Read output files            

    fid = fopen([datafilename 'wtest.dat'],'r');
    %disp (size(data.testmatrix));
    %disp (size(data.trainmatrix));
    results.(timecoursevariant).weights{SVMpass}        = fread(fid,size(data.testmatrix,1),'float'); 
    fclose(fid);
    results.(timecoursevariant).novoxels{SVMpass}       = cfg.NoVox;
    results.(timecoursevariant).SVMAccuracies{SVMpass}  = textread([ datafilename 'accuracy.dat']);
    results.(timecoursevariant).predTimecourse{SVMpass} = textread([ testdatafile '.pred']);
    delete(traindatafile);
    delete(testdatafile);
    delete([testdatafile '.pred']);
    delete([traindatafile '.svm']);
    delete([datafilename 'wtest.dat']);
    delete([datafilename 'accuracy.dat']);
    crashed = 0;
catch ME
    disp(ME.message);
    disp('winSVM crashed, retrying');
    crashed = 1;   
    delete(traindatafile);
    delete(testdatafile);
    delete([testdatafile '.pred']);
    delete([traindatafile '.svm']);
    delete([datafilename 'wtest.dat']);
    delete([datafilename 'accuracy.dat']);end
end

results.minclass                          = 1; % Minority class
results.(timecoursevariant).nodetected(SVMpass)    = sum(sign(results.(timecoursevariant).predTimecourse{SVMpass}) == results.minclass);
results.(timecoursevariant).nopresent(SVMpass)     = sum(data.testevents == results.minclass);
        
stoploop = 0;

    ypred2 = results.(timecoursevariant).predTimecourse{SVMpass};
    while (results.(timecoursevariant).nodetected(SVMpass) ~= results.(timecoursevariant).nopresent(SVMpass)) && (stoploop <= 19999)

        if (results.(timecoursevariant).nodetected(SVMpass) >= results.(timecoursevariant).nopresent(SVMpass)) && (stoploop <= 19998)
            ypred2 = ypred2 - 0.001;
        elseif results.(timecoursevariant).nodetected(SVMpass) <= results.(timecoursevariant).nopresent(SVMpass)
            ypred2 = ypred2 + 0.001;
        end
            results.(timecoursevariant).nodetected(SVMpass) = sum(ypred2 > 0);
            stoploop = stoploop + 1;
    end 
results.(timecoursevariant).scaledtestprediction{SVMpass} = sign(ypred2);
results.(timecoursevariant).testprediction{SVMpass}       = sign(results.(timecoursevariant).predTimecourse{SVMpass});



end